﻿import requests,json
from bs4 import BeautifulSoup as bs
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}
url='http://data.sports.sohu.com/nba/nba_players_rank.php?order_by=points&spm=smpc.fb-nba-home.top-dc.2.1620824904040nf6byr7'
re=requests.get(url=url,headers=headers)
re.encoding='GBK'
html = re.text
soup = bs(html,"html.parser")
ts = ["排名","球员","球队","出场次数","场均时间","得分","命中/出手","命中率","命中/三分","三分命中率","命中/罚球","罚球命中率"]
table = soup.table
ls = []
trs = table.find_all("tr")
for tr in trs:
    tds = tr.find_all("td")
    map = {}
    i = 0
    for td in tds:
        map.update({ts[i]:td.text})
        i += 1
    ls.append(map)
lss = json.dumps(ls,ensure_ascii=False)
f = open("NBA.txt","w")
f.write(lss)
f.close()
